home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 014 / pdterm / terminal.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  11KB  |  357 lines

  1.  
  2. /*  A simple terminal emulator.  Does ANSI/DEC VT-100 emulation in
  3.     80 cols by 25 lines.
  4.  
  5.     by  Michael J. McInerny   21-Feb-86 version 1.21
  6.  
  7. */
  8.  
  9. #define INTUITION_MESSAGE (1<<intuitionMsgBit)
  10. #define TYPED_CHARACTER (1<<consoleReadBit)
  11. #define INPUT_CHARACTER (1<<serReadBit)
  12.  
  13. #define CloseConsole(x) CloseDevice(x)
  14.  
  15. #include "term.h"
  16.  
  17. /* GLOBALS ****************************************************** */
  18. long IntuitionBase=0;
  19. long GfxBase=0;
  20.  
  21. struct Window *TerminalWindow;           /* "Public" window handle     */
  22. struct Menu *MenuHead;
  23. struct NewWindow nw = {
  24.    0, 0,             /* start position                  */
  25.    640, 200,         /* width, height                   */
  26.    -1, -1,           /* detail pen, block pen           */
  27.    MENUPICK,
  28.                      /* IDCMP flags                     */
  29.    ACTIVATE | BORDERLESS,
  30.                      /* window flags                    */
  31.    NULL,             /* pointer to first user gadget    */
  32.    NULL,             /* pointer to user checkmark       */
  33.    NULL,             /* window title                    */
  34.    NULL,             /* pointer to screen    (later)    */
  35.    NULL,             /* pointer to superbitmap          */
  36.    50,40,640,200,    /* sizing limits min and max       */
  37.    WBENCHSCREEN      /* type of screen in which to open */
  38.    };
  39.  
  40. struct MsgPort *consoleWritePort;
  41. struct MsgPort *consoleReadPort;
  42. struct MsgPort *serReadPort;
  43. struct MsgPort *serWritePort;
  44. struct IOStdReq *ConWriteReq;
  45. struct IOStdReq *ConReadReq;
  46. struct IOExtSer *SerReadReq;
  47. struct IOExtSer *SerWriteReq;
  48.  
  49. char letter;            /* one letter at a time from console */
  50. char serin;            /* one letter at a time from serial */
  51.  
  52. InitWindow()
  53. {
  54.    GfxBase = OpenLibrary("graphics.library", 0);
  55.    if (GfxBase == NULL) Cleanup(1);
  56.    IntuitionBase = OpenLibrary("intuition.library", 0);
  57.    if (IntuitionBase == NULL) Cleanup(2);
  58.    TerminalWindow = OpenWindow(&nw);
  59.    if ( TerminalWindow == NULL ) Cleanup(3);
  60. }
  61.  
  62. InitMenus()
  63. {
  64.    struct Menu *CurrentMenu, *NewMenu(), *AddMenu();
  65.    struct MenuItem *CurrentItem, *SubItem,
  66.                    *AddNewMenuItem(), *AddItem(), *AddNewSubItem();
  67.  
  68.    CurrentMenu    = NewMenu("Project", 60, 10);
  69.    MenuHead       = CurrentMenu;
  70.       CurrentItem = AddNewMenuItem(CurrentMenu, "About PDTerm",100,11);
  71.       CurrentItem = AddItem(CurrentItem, "Window");
  72.          SubItem  = AddNewSubItem(CurrentItem, "to Back",68,11);
  73.          SubItem  = AddItem(SubItem,"to Front");
  74.       CurrentItem = AddItem(CurrentItem, "Quit");
  75.    CurrentMenu    = AddMenu(CurrentMenu, "Settings",68,10);
  76.       CurrentItem = AddNewMenuItem(CurrentMenu,"Baud",52,11);
  77.          SubItem  = AddNewSubItem(CurrentItem, "   300   ",76,11);
  78.          SubItem->MutualExclude = (~(1 << 0));
  79.          SubItem->Flags |= CHECKIT;
  80.          SubItem  = AddItem(SubItem,"  1200   ");
  81.          SubItem->MutualExclude = (~(1 << 1));
  82.          SubItem->Flags |= CHECKIT | CHECKED;
  83.          SubItem  = AddItem(SubItem,"  2400   ");
  84.          SubItem->MutualExclude = (~(1 << 2));
  85.          SubItem->Flags |= CHECKIT;
  86.          SubItem  = AddItem(SubItem,"  4800   ");
  87.          SubItem->MutualExclude = (~(1 << 3));
  88.          SubItem->Flags |= CHECKIT;
  89.          SubItem  = AddItem(SubItem,"  9600   ");
  90.          SubItem->MutualExclude = (~(1 << 4));
  91.          SubItem->Flags |= CHECKIT;
  92.       CurrentItem = AddItem(CurrentItem,"Length");
  93.          SubItem  = AddNewSubItem(CurrentItem,"   7 bits   ",100,11);
  94.          SubItem->MutualExclude = (~(1 << 0));
  95.          SubItem->Flags |= CHECKIT | CHECKED;
  96.          SubItem  = AddItem(SubItem,"   8 bits   ");
  97.          SubItem->MutualExclude = (~(1 << 1));
  98.          SubItem->Flags |= CHECKIT;
  99.  
  100.    SetMenuStrip( TerminalWindow, MenuHead);
  101. }
  102.  
  103. InitReqs()
  104. {
  105.    consoleWritePort = CreatePort("my.con.write",0);
  106.    if(consoleWritePort == 0) Cleanup(4);
  107.    ConWriteReq = CreateStdIO(consoleWritePort);
  108.    if(ConWriteReq == 0) Cleanup(5);
  109.    consoleReadPort = CreatePort("my.con.read",0);
  110.    if(consoleReadPort == 0) Cleanup(6);
  111.    ConReadReq =  CreateStdIO(consoleReadPort);
  112.    if(ConReadReq == 0) Cleanup(7);
  113.    if((OpenConsole(ConWriteReq,ConReadReq,TerminalWindow)) != 0)
  114.       Cleanup(8);
  115.  
  116.    serReadPort = CreatePort("my.ser.read",0);
  117.    if (serReadPort == NULL) Cleanup(9);
  118.    SerReadReq = (struct IOExtSer *)CreateExtIO(serReadPort,
  119.                                                sizeof(struct IOExtSer));
  120.    if (SerReadReq == NULL) Cleanup(10);
  121.    serWritePort = CreatePort("my.ser.write",0);
  122.    if (serWritePort == NULL) Cleanup(11);
  123.    SerWriteReq = (struct IOExtSer *)CreateExtIO(serWritePort,
  124.                                                sizeof(struct IOExtSer));
  125.    if (SerWriteReq == NULL) Cleanup(12);
  126.    if ((OpenSerial(SerReadReq, SerWriteReq)) != 0) Cleanup(13);
  127.    if ((SetParams( SerReadReq, 4096, 0x07, 0x07, 750000,
  128.                            1200, NULL, 0x51040303, 0x03030303)) != 0)
  129.       Cleanup(14);
  130. }
  131.  
  132. main()
  133. {
  134.    USHORT class, code, qualifier;
  135.    int problem, wakeupmask, consoleReadBit, intuitionMsgBit, serReadBit;
  136.    struct IntuiMessage *message; /* the message the IDCMP sends us */
  137.  
  138.    InitWindow();
  139.    InitMenus();
  140.    InitReqs();
  141.  
  142.    QueueRead(ConReadReq,&letter);
  143.    QueueSerRead(SerReadReq, &serin);
  144.  
  145.    consoleReadBit = ConReadReq->io_Message.mn_ReplyPort->mp_SigBit;
  146.    intuitionMsgBit = TerminalWindow->UserPort->mp_SigBit;
  147.    serReadBit = SerReadReq->IOSer.io_Message.mn_ReplyPort->mp_SigBit;
  148.  
  149.    do
  150.    {
  151.       wakeupmask = Wait( INTUITION_MESSAGE |
  152.                          TYPED_CHARACTER |
  153.                          INPUT_CHARACTER);
  154.  
  155.       while(CheckIO(ConReadReq))
  156.          {
  157.             WaitIO(ConReadReq);
  158.             SerPutChar(SerWriteReq,letter);
  159.             QueueRead(ConReadReq, &letter);
  160.          }
  161.       while(CheckIO(SerReadReq))
  162.          {
  163.             WaitIO(SerReadReq);
  164.             ConPutChar(ConWriteReq,serin);
  165.             QueueSerRead(SerReadReq, &serin);
  166.          }
  167.       if(wakeupmask & INTUITION_MESSAGE)
  168.          {
  169.             while((message = (struct IntuiMessage *)
  170.                   GetMsg(TerminalWindow->UserPort) ) != NULL)
  171.                {
  172.                   class     = message->Class;
  173.                   code      = message->Code;
  174.                   qualifier = message->Qualifier;
  175.                   ReplyMsg(message);
  176.                   problem = HandleEvent(class,code,qualifier);
  177.                   if(problem == FALSE) break;
  178.                }
  179.          }
  180.    } while (problem); /* keep going as long as HandleEvent returns nonzero */
  181.    AbortIO(ConReadReq);      /* cancel the last queued read */
  182.    AbortIO(SerReadReq);
  183.    Cleanup(0);
  184. }
  185.  
  186. Cleanup(problem)
  187. int problem;
  188. {
  189.    if ((problem >= 14) || (problem == 0)) CloseDevice(SerReadReq);
  190.    if ((problem >= 13) || (problem == 0))
  191.       DeleteExtIO(SerWriteReq,sizeof(struct IOExtSer));
  192.    if ((problem >= 12) || (problem == 0)) DeletePort(serWritePort);
  193.    if ((problem >= 11) || (problem == 0))
  194.       DeleteExtIO(SerReadReq,sizeof(struct IOExtSer));
  195.    if ((problem >= 10) || (problem == 0)) DeletePort(serReadPort);
  196.    if ((problem >= 9) || (problem == 0)) CloseConsole(ConWriteReq);
  197.    if ((problem >= 8) || (problem == 0)) DeleteStdIO(ConReadReq);
  198.    if ((problem >= 7) || (problem == 0)) DeletePort(consoleReadPort);
  199.    if ((problem >= 6) || (problem == 0)) DeleteStdIO(ConWriteReq);
  200.    if ((problem >= 5) || (problem == 0)) DeletePort(consoleWritePort);
  201.    if ((problem >= 4) || (problem == 0)) {
  202.       ClearMenuStrip(TerminalWindow);
  203.       DisposeMenus(MenuHead);
  204.       CloseWindow(TerminalWindow);
  205.       }
  206.    if ((problem >= 3) || (problem == 0)) CloseLibrary(IntuitionBase);
  207.    if ((problem >= 2) || (problem == 0)) CloseLibrary(GfxBase);
  208.    if(problem > 0)
  209.          exit(problem+1000);
  210.    else
  211.          return(0);
  212. }
  213.  
  214. HandleEvent(class,code,qualifier)
  215. USHORT class;
  216. USHORT code;
  217. USHORT qualifier;
  218. {
  219.       switch(class) {
  220.          case MENUPICK:
  221.             return(MenuSwitch(code));
  222.             break;
  223.       } /* end of switch( class ) */
  224.       return(TRUE);
  225. } /* end of HandleEvent */
  226.  
  227. MenuSwitch(code)
  228. USHORT code;
  229. {
  230.    USHORT menunum;
  231.    struct MenuItem *item;
  232.    int error;
  233.  
  234.    error = TRUE;
  235.    while(code != MENUNULL ) {
  236.       item = (struct MenuItem *)ItemAddress(MenuHead, code);
  237.       menunum = MENUNUM( code );
  238.       switch( menunum ) {
  239.          case 0:
  240.             error &= ProjectMenu(code);
  241.             break;
  242.          case 1:
  243.             error &= SettingsMenu(code);
  244.             break;
  245.       } /* end of switch ( menunum ) */
  246.       code = item->NextSelect;
  247.    } /* end of while (code != MENUNULL) */
  248.    return(error);
  249. } /* end of MenuSwitch */
  250.  
  251.  
  252. ProjectMenu(code)
  253. USHORT code;
  254. {
  255.    USHORT itemnum;
  256.    struct IntuiText *InfoText, *OKText, *NewIText(), *AddIText();
  257.    itemnum = ITEMNUM( code );
  258.    switch( itemnum ) {
  259.       case 0:  /* About PDTerm */
  260.          InfoText = NewIText("Public Domain Terminal Emulator",12,5);
  261.          AddIText(InfoText, "    by Michael McInerny  ");
  262.          OKText = NewIText("Okay",6,3);
  263.          AutoRequest(TerminalWindow,
  264.                      InfoText,
  265.                      NULL, OKText,
  266.                      NULL, NULL,
  267.                      296, 65);
  268.          DisposeIText(InfoText);
  269.          DisposeIText(OKText);
  270.          return(TRUE);
  271.       case 1:  /* Window */
  272.          return(ArrangeMenu(code));
  273.          break;
  274.       case 2: /* Quit */
  275.          return( FALSE );
  276.          break;
  277.    } /* end of switch ( itemnum ) */
  278.    return( TRUE );
  279. } /* end of ProjectMenu */
  280.  
  281. ArrangeMenu(code)
  282. USHORT code;
  283. {
  284.    USHORT subitem;
  285.    subitem = SUBNUM( code );
  286.    switch( subitem ) {
  287.       case 0:
  288.          WindowToBack( TerminalWindow );
  289.          break;
  290.       case 1:
  291.          WindowToFront( TerminalWindow );
  292.          break;
  293.    } /* end of switch ( subitem ) */
  294.    return( TRUE );
  295. } /* end of ArrangeMenu */
  296.  
  297. SettingsMenu(code)
  298. USHORT code;
  299. {
  300.    USHORT itemnum;
  301.    itemnum = ITEMNUM( code );
  302.    AbortIO(SerReadReq);
  303.    switch ( itemnum ) {
  304.       case 0:
  305.          BaudMenu(code);
  306.          break;
  307.       case 1:
  308.          LengthMenu(code);
  309.          break;
  310.    } /* end of switch ( itemnum ) */
  311.    SerReadReq->IOSer.io_Command = SDCMD_SETPARAMS;
  312.    DoIO(SerReadReq);
  313.    QueueSerRead(SerReadReq, &serin);
  314.    return( TRUE );
  315. } /* end of SettingsMenu */
  316.  
  317. BaudMenu(code)
  318. USHORT code;
  319. {
  320.    USHORT subitem;
  321.    subitem = SUBNUM( code );
  322.    switch( subitem ) {
  323.       case 0:
  324.          SerReadReq->io_Baud = 300;
  325.          break;
  326.       case 1:
  327.          SerReadReq->io_Baud = 1200;
  328.          break;
  329.       case 2:
  330.          SerReadReq->io_Baud = 2400;
  331.          break;
  332.       case 3:
  333.          SerReadReq->io_Baud = 4800;
  334.          break;
  335.       case 4:
  336.          SerReadReq->io_Baud = 9600;
  337.          break;
  338.    } /* end of switch ( subitem ) */
  339. } /* end of BaudMenu */
  340.  
  341. LengthMenu(code)
  342. USHORT code;
  343. {
  344.    USHORT subitem;
  345.    subitem = SUBNUM( code );
  346.    switch( subitem ) {
  347.       case 0:
  348.          SerReadReq->io_ReadLen = 0x07;
  349.          SerReadReq->io_WriteLen = 0x07;
  350.          break;
  351.       case 1:
  352.          SerReadReq->io_ReadLen = 0x08;
  353.          SerReadReq->io_WriteLen = 0x08;
  354.          break;
  355.    } /* end of switch ( subitem ) */
  356. } /* end of LengthMenu */
  357.